﻿using Sx.Mall.Domain;
using Sx.Mall.WebContract;
using Sx.Mall.WebContract.Dto;
using Sx.Mall.WebService.Common;
using Mall.Infrastructure.Model;
using Mall.Infrastructure.Repository;
using Mall.Infrastructure.EnityFramework;
using Mall.Infrastructure.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Sx.Mall.WebService
{
    public class ProductService : IProductService
    {
        private readonly AppDomain app = AppDomain.CurrentDomain;

        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public CommonResult Add(ProductDto dto)
        {
            CommonResult result = new CommonResult();
            using (IRepositoryContext ctx = app.GetRepositoryContext())
            {
                var Repository = ctx.GetRepository<Product>();
                var entity = dto.Convert<ProductDto, Product>();
                result = Product.Add(Repository, entity);
                ctx.Commit();
            }
            return result;
        }


        /// <summary>
        /// 修改产品
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public CommonResult Update(ProductDto dto)
        {
            CommonResult result = new CommonResult();
            using (IRepositoryContext ctx = app.GetRepositoryContext())
            {
                var Repository = ctx.GetRepository<Product>();
                var entity = dto.Convert<ProductDto, Product>();
                result = Product.Update(Repository, entity);
                ctx.Commit();
            }
            return result;
        }

        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public CommonResult Delete(List<int> ids)
        {
            CommonResult result = new CommonResult();
            using (IRepositoryContext ctx = app.GetRepositoryContext())
            {
                var userRepository = ctx.GetRepository<Product>();
                result = Product.Delete(userRepository, ids);
                ctx.Commit();
            }
            return result;
        }



        /// <summary>
        /// 查询产品
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public PageResult<ProductDto> GetList(ProductDto dto)
        {
            Page page = dto.Page;
            PageResult<ProductDto> result = new PageResult<ProductDto>();
            Expression<Func<Product, bool>> find = m => true;

            if (!string.IsNullOrWhiteSpace(dto.ProductName))
            {
                //根据产品名称模糊查找
                find = find.And(m => m.ProductName.Contains(dto.ProductName));
            }

            using (IRepositoryContext ctx = app.GetRepositoryContext())
            {
                var productRepository = ctx.GetRepository<Product>();

                var item = (from a in productRepository.Where(find)
                            select new ProductDto
                            {
                                bigTypeId=a.bigTypeId,
                                CreateID=a.CreateID,
                                CreateName=a.CreateName,
                                CreateTime=a.CreateTime,
                                Description=a.Description,
                                Discount=a.Discount,
                                DiscountLimit=a.DiscountLimit,
                                KeyWord=a.KeyWord,
                                NodeID=a.NodeID,
                                price=a.price,
                                ProductContent=a.ProductContent,
                                ProductID=a.ProductID,
                                ProductName=a.ProductName,
                                ProductParam=a.ProductParam,
                                ProductTags=a.ProductTags,
                                proPic=a.proPic,
                                smallTypeId=a.smallTypeId,
                                specialPrice=a.specialPrice,
                                specialPriceTime=a.specialPriceTime,
                                stock=a.stock,
                                UpdateID=a.UpdateID,
                                UpdateName=a.UpdateName,
                                UpdateTime=a.UpdateTime,
                                Status = a.Status,

                            });
                result.rows = item.OrderBy(m => m.ProductID).Paging(page).ToList();
                result.total = page.Count;
            }
            return result;
        }

    }
}
